package com.fulihui.usercore.core.repository.impl;

import com.alibaba.fastjson.JSONObject;
import com.fulihui.usercore.biz.conv.CooperationInfoConv;
import com.fulihui.usercore.biz.conv.MerchantConv;
import com.fulihui.usercore.condition.MerchantCondition;
import com.fulihui.usercore.core.repository.MerchantRepository;
import com.fulihui.usercore.dal.dataobj.CooperationInfoDO;
import com.fulihui.usercore.dal.dataobj.MerchantDO;
import com.fulihui.usercore.dal.mapper.CooperationInfoMapper;
import com.fulihui.usercore.dal.mapper.MerchantMapper;
import com.fulihui.usercore.dto.MerchantDTO;
import com.fulihui.usercore.request.merchant.MerchantSaveRequest;
import com.fulihui.usercore.request.merchant.UpdateStateRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.UUID;

/**
 * @author Willard.Hu on 2016/9/25.
 */
@Repository
public class MerchantRepositoryImpl implements MerchantRepository {
    @Autowired
    private MerchantMapper merchantMapper;
    @Autowired
    private CooperationInfoMapper cooperationInfoMapper;

    @Override
    public String insert(MerchantSaveRequest request) {
        String mid = "M" + String.valueOf(UUID.randomUUID());
        request.setMid(mid);
        merchantMapper.insert(MerchantConv.toDO(request, null));
        return mid;
    }

    @Override
    public boolean update(MerchantSaveRequest request) {
        MerchantDO old = merchantMapper.queryByPK(request.getMid());
        return merchantMapper.update(MerchantConv.toDO(request,
                JSONObject.parseObject(old.getExtInfo())));
    }

    @Override
    public boolean updateState(UpdateStateRequest request) {
        MerchantDO upate = new MerchantDO();
        upate.setMid(request.getMid());
        upate.setState(request.getState());
        return merchantMapper.update(upate);
    }

    @Override
    public MerchantDTO queryByPK(String mid) {
        MerchantDTO dto = MerchantConv.toDTO(merchantMapper.queryByPK(mid));
        assemble(dto);
        return dto;
    }

    @Override
    public MerchantDTO queryByName(String name, String state) {
        MerchantDO params = new MerchantDO();
        params.setName(name);
        params.setState(state);
        List<MerchantDO> list = merchantMapper.query(params);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        MerchantDTO dto = MerchantConv.toDTO(list.get(0));
        assemble(dto);
        return dto;
    }

    @Override
    public List<MerchantDTO> queryPage(int start, int rows, MerchantCondition condition) {
        List<MerchantDO> dolist = merchantMapper.queryPage(start, rows,
                MerchantConv.toDO(condition));
        List<MerchantDTO> dtoList = MerchantConv.toList(dolist);
        dtoList.forEach(this::assemble);
        return dtoList;
    }

    @Override
    public int totalCount(MerchantCondition condition) {
        return merchantMapper.totalCount(MerchantConv.toDO(condition));
    }

    private void assemble(MerchantDTO dto) {
        CooperationInfoDO params = new CooperationInfoDO();
        params.setMid(dto.getMid());
        List<CooperationInfoDO> list = cooperationInfoMapper.query(params);
        // 按时间倒序排序的合作方式，第一条就是最新的
        dto.setNewestOne(CollectionUtils.isEmpty(list) ? null : CooperationInfoConv.toDTO(list
                .get(0)));
    }
}
